{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "from datetime import datetime\n",
    "import openpyxl as px\n",
    "import calendar\n",
    "\n",
    "def zcq(month): #中长期持仓分析\n",
    "    \n",
    "    import calendar\n",
    "\n",
    "\n",
    "    df_list = []\n",
    "\n",
    "    year=2024\n",
    "    \n",
    "    _,max_day = calendar.monthrange(year, month)\n",
    "    \n",
    "    # 遍历日期\n",
    "    for day in range(1, max_day + 1):\n",
    "        # 遍历时段\n",
    "        for minute in range(15, 24 * 60+15 , 15):  # 从00:15开始，包含24:00\n",
    "            hour_1 = minute // 60\n",
    "            hour = (minute-15) // 60\n",
    "            minute %= 60\n",
    "            # 将月份、日期和时段添加到数据框列表中，分钟从00:00分开始，24:00结束\n",
    "            if hour_1 != 24:  # 不包含24:00\n",
    "                if hour == 23:              \n",
    "                    if minute == 0:\n",
    "                        df_list.append(pd.DataFrame({'月份': [month], '日期': [day],'小时':[f'{hour%24:02d}'] ,'分钟':[f'{(hour+1)%24:02d}:{minute:02d}'],'分时段类型': ['23:00-24:00']}))\n",
    "                    else:\n",
    "                        df_list.append(pd.DataFrame({'月份': [month], '日期': [day],'小时':[f'{hour%24:02d}'] ,'分钟':[f'{hour%24:02d}:{minute:02d}'],'分时段类型': ['23:00-24:00']}))  \n",
    "                else:\n",
    "                    if minute == 0:\n",
    "                        df_list.append(pd.DataFrame({'月份': [month], '日期': [day],'小时':[f'{hour%24:02d}'] ,'分钟':[f'{(hour+1)%24:02d}:{minute:02d}'],'分时段类型': [f'{hour%24:02d}:00-{(hour+1)%24:02d}:00']}))\n",
    "                    else:\n",
    "                        df_list.append(pd.DataFrame({'月份': [month], '日期': [day],'小时':[f'{hour%24:02d}'] ,'分钟':[f'{hour%24:02d}:{minute:02d}'],'分时段类型': [f'{hour%24:02d}:00-{(hour+1)%24:02d}:00']}))  \n",
    "            else:\n",
    "                df_list.append(pd.DataFrame({'月份': [month], '日期': [day],'小时':['23'] ,'分钟':['24:00'],'分时段类型': ['23:00-24:00']}))  # 包含24:00\n",
    "    # 使用pd.concat连接数据框列表\n",
    "    df = pd.concat(df_list, ignore_index=True)\n",
    "\n",
    "    # 将数据框写入Excel文件\n",
    "    #df.to_excel('2024中长期持仓.xlsx', index=False)\n",
    "    return df\n",
    "  \n",
    "\n",
    "def year_zcq(df1,sheeet_name,n_month): #年度中长期电量\n",
    "    # 读取数据2024中长期持仓\n",
    "    #df1= pd.read_excel('2024中长期持仓.xlsx')\n",
    "    # 读取中长期持仓汇总表文件\n",
    "    df2= pd.read_excel(file_zcq, sheet_name=sheeet_name, header=1)\n",
    "\n",
    "    # 选择第1行至第25行的数据\n",
    "    df2= df2.iloc[1:25]\n",
    "\n",
    "    # 在df1中增加两列\n",
    "    df1['年度中长期1-交易电量'] = None\n",
    "    df1['年度中长期1-电价'] = None\n",
    "\n",
    "    # 将df2的数据按照分时段类型与df1的时段类型相匹配\n",
    "    df1 = df1.merge(df2[['分时段类型', '交易电量', '电价']], how='left', left_on='分时段类型', right_on='分时段类型')\n",
    "\n",
    "    # 将df2中的交易电量和电价放到df1中月份为2的“年度中长期1-交易电量”和“年度中长期2-电价”数据中\n",
    "    df1.loc[df1['月份'] == n_month, '年度中长期1-交易电量'] = df1['交易电量']/4\n",
    "    df1.loc[df1['月份'] == n_month,'年度中长期1-电价'] = df1['电价']\n",
    "\n",
    "    # 删除不需要的列\n",
    "    df1 = df1.drop(['交易电量', '电价'], axis=1)\n",
    "    return df1\n",
    "\n",
    "def ws_zcq(df1,sheet_name,n_month,max_day): #外送中长期电量\n",
    "    # 读取中长期持仓汇总表文件中的外送1，标题在第38行，列从B开始，一共29列\n",
    "    \n",
    "    df3= pd.read_excel(file_zcq, sheet_name=sheet_name, header=37, usecols='B:AG')\n",
    "    df3=df3.iloc[:,0:max_day+1]\n",
    "    df3=pd.melt(df3, id_vars=['日期'], value_vars=df3.columns[1:], var_name='分时段类型', value_name='交易电量')\n",
    "    # 将df3的各列列名分别改为'分时段类型''日期’和‘交易电量’\n",
    "    df3.columns = ['分时段类型', '日期', '交易电量']\n",
    "    \n",
    "    # df3增加两列，分别为月和日，月列数据和日列数据分别从日期列中的日期数据转换而来\n",
    "    df3['月份'] = pd.to_datetime(df3['日期']).dt.month\n",
    "    df3['日期'] = pd.to_datetime(df3['日期']).dt.day\n",
    "    # 读取中称期持仓汇总表文件中\"外送1\"工作表的\"C36单元格\"数据，将其存储到ws_name1中\n",
    "    wb = px.load_workbook(file_zcq)\n",
    "    ws_name1 = wb[sheet_name]\n",
    "    ws_name1 = ws_name1['C36'].value\n",
    "    # 读取\"G36\"单元格数据，将其存储到ws_price1中\n",
    "    ws_price1 = wb[sheet_name]\n",
    "    ws_price1 = ws_price1['A2'].value\n",
    "    # 在df1中增加两列，分别为f'外送{ws_name1}-交易电量'和f'外送{ws_name1}-电价'\n",
    "    df1[f'外送{ws_name1}-交易电量'] = None\n",
    "    df1[f'外送{ws_name1}-电价'] = None\n",
    "    # 将df3的数据按照分时段类型与df1的时段类型、月、日相匹配\n",
    "    df1 = df1.merge(df3[['分时段类型', '月份', '日期', '交易电量']], how='left', left_on=['分时段类型', '月份', '日期'], right_on=['分时段类型', '月份', '日期'])\n",
    "    # 将df3中的交易电量放到df1中月份为1的f'外送{ws_name1}-交易电量'数据中\n",
    "    df1.loc[df1['月份'] == n_month, f'外送{ws_name1}-交易电量'] = df1['交易电量']/4\n",
    "    #将df1中的f'外送{ws_name1}-交易电价'数据中的空值填充为ws_price1\n",
    "    df1.loc[df1['月份'] == n_month, f'外送{ws_name1}-电价'] = ws_price1\n",
    "    # 删除不需要的列\n",
    "    df1 = df1.drop(['交易电量'], axis=1)\n",
    "    return df1\n",
    "def jz_yue(df1,sheet_name,n_month,y_name): # 月度集中竞价\n",
    "    # 读取中长期持仓汇总表文件中的月、旬集中竞价，标题在第2行，数据从B列到F列，从第4行到第27行，标题数据为字符型\n",
    "    df3= pd.read_excel(file_zcq, sheet_name=sheet_name, usecols='B:F', skiprows=2, nrows=24)\n",
    "    #df3列名为'分时段类型'，'月份'，'交易电量'，'电量'，电价'\n",
    "    df3.columns = ['分时段类型', '月份', '交易电量','电量', '电价']\n",
    "    #删除月份列和电量列\n",
    "    df3 = df3.drop(['月份', '电量'], axis=1)\n",
    "    #在df1中增加两列，分别为'月度集中竞价-交易电量'和'月度集中竞价-电价'\n",
    "    df1[f'{y_name}-交易电量'] = None\n",
    "    df1[f'{y_name}-电价'] = None\n",
    "    #将df3的数据按照分时段类型与df1的时段类型相匹配\n",
    "    df1 = df1.merge(df3[['分时段类型', '交易电量', '电价']], how='left', left_on='分时段类型', right_on='分时段类型')\n",
    "    #将df3中的交易电量和电价放到df1中月份为1的'月度集中竞价-交易电量'和'月度集中竞价-电价'数据中\n",
    "    df1.loc[df1['月份'] == n_month, f'{y_name}-交易电量'] = df1['交易电量']/4\n",
    "    df1.loc[df1['月份'] == n_month, f'{y_name}-电价'] = df1['电价']\n",
    "    #删除不需要的列\n",
    "    df1 = df1.drop(['交易电量', '电价'], axis=1)\n",
    "    return df1\n",
    "def jz_sx(df1,sheet_name,n_month,y_name):#上旬集中竞价\n",
    "        \n",
    "    # 读取中长期持仓汇总表文件中的月、旬集中竞价，标题在第2行，数据从I列到M列，从第4行到第27行，标题数据为字符型\n",
    "    df3= pd.read_excel(file_zcq, sheet_name=sheet_name, usecols='I:M', skiprows=2, nrows=24)\n",
    "    #df3列名为'分时段类型'，'月份'，'交易电量'，'电量'，电价'\n",
    "    df3.columns = ['分时段类型', '月份', '交易电量','电量', '电价']\n",
    "    #删除月份列和电量列\n",
    "    df3 = df3.drop(['月份', '电量'], axis=1)\n",
    "    #在df1中增加两列，分别为'上旬集中竞价-交易电量'和'上旬集中竞价-电价'\n",
    "    df1[f'{y_name}-交易电量'] = None\n",
    "    df1[f'{y_name}-电价'] = None\n",
    "    #将df3的数据按照分时段类型与df1的时段类型相匹配\n",
    "    df1 = df1.merge(df3[['分时段类型', '交易电量', '电价']], how='left', left_on='分时段类型', right_on='分时段类型')\n",
    "    #将df3中的交易电量和电价放到df1中月份为1的'上旬集中竞价-交易电量'和'上旬集中竞价-电价'数据中，交易电量除以4，月份为1，日期为1-10\n",
    "    df1.loc[(df1['月份'] == n_month) & (df1['日期'] <= 10), f'{y_name}-交易电量'] = df1['交易电量']/4\n",
    "    df1.loc[(df1['月份'] == n_month) & (df1['日期'] <= 10), f'{y_name}-电价'] = df1['电价']\n",
    "    #删除不需要的列\n",
    "    df1 = df1.drop(['交易电量', '电价'], axis=1)\n",
    "    return df1\n",
    "def jz_zx(df1,sheet_name,n_month,y_name): #中旬集中竞价\n",
    "    # 读取中长期持仓汇总表文件中的月、旬集中竞价，标题在第2行，数据从P列到T列，从第4行到第27行，标题数据为字符型\n",
    "    df3= pd.read_excel(file_zcq, sheet_name=sheet_name, usecols='P:T', skiprows=2, nrows=24)\n",
    "    #df3列名为'分时段类型'，'月份'，'交易电量'，'电量'，电价'\n",
    "    df3.columns = ['分时段类型', '月份', '交易电量','电量', '电价']\n",
    "    #删除月份列和电量列\n",
    "    df3 = df3.drop(['月份', '电量'], axis=1)\n",
    "    #在df1中增加两列，分别为'中旬集中竞价-交易电量'和'中旬集中竞价-电价'\n",
    "    df1[f'{y_name}-交易电量'] = None\n",
    "    df1[f'{y_name}-电价'] = None\n",
    "    #将df3的数据按照分时段类型与df1的时段类型相匹配\n",
    "    df1 = df1.merge(df3[['分时段类型', '交易电量', '电价']], how='left', left_on='分时段类型', right_on='分时段类型')\n",
    "    #将df3中的交易电量和电价放到df1中月份为1的'上旬集中竞价-交易电量'和'上旬集中竞价-电价'数据中，交易电量除以4，月份为1，日期从11到20\n",
    "    df1.loc[(df1['月份'] == n_month) & (df1['日期'] <= 20) & (df1['日期']>10), f'{y_name}-交易电量'] = df1['交易电量']/4\n",
    "    df1.loc[(df1['月份'] == n_month) & (df1['日期'] <= 20) & (df1['日期']>10), f'{y_name}-电价'] = df1['电价']\n",
    "    \n",
    "    #删除不需要的列\n",
    "    df1 = df1.drop(['交易电量', '电价'], axis=1)\n",
    "    return df1\n",
    "def jz_xx(df1,sheet_name,n_month,y_name): #下旬集中竞价\n",
    "    # 读取中长期持仓汇总表文件中的月、旬集中竞价，标题在第2行，数据从W列到AA列，从第4行到第27行，标题数据为字符型\n",
    "    df3= pd.read_excel(file_zcq, sheet_name=sheet_name, usecols='W:AA', skiprows=2, nrows=24)\n",
    "    #df3列名为'分时段类型'，'月份'，'交易电量'，'电量'，电价'\n",
    "    df3.columns = ['分时段类型', '月份', '交易电量','电量', '电价']\n",
    "    #删除月份列和电量列\n",
    "    df3 = df3.drop(['月份', '电量'], axis=1)\n",
    "    #在df1中增加两列，分别为'下旬集中竞价-交易电量'和'下旬集中竞价-电价'\n",
    "    df1[f'{y_name}-交易电量'] = None\n",
    "    df1[f'{y_name}-电价'] = None\n",
    "    #将df3的数据按照分时段类型与df1的时段类型相匹配\n",
    "    df1 = df1.merge(df3[['分时段类型', '交易电量', '电价']], how='left', left_on='分时段类型', right_on='分时段类型')\n",
    "    #将df3中的交易电量和电价放到df1中月份为1的'上旬集中竞价-交易电量'和'上旬集中竞价-电价'数据中，交易电量除以4，月份为1，日期从21日到月末\n",
    "    df1.loc[(df1['月份'] == n_month) & (df1['日期'] > 20), f'{y_name}-交易电量'] = df1['交易电量']/4\n",
    "    df1.loc[(df1['月份'] == n_month) & (df1['日期'] > 20), f'{y_name}-电价'] = df1['电价']\n",
    "     \n",
    "    #删除不需要的列\n",
    "    df1 = df1.drop(['交易电量', '电价'], axis=1)\n",
    "    return df1\n",
    "def rgd(df1,sheet_name,n_month): #日滚动撮合\n",
    "    tf=pd.read_excel(file_zcq, sheet_name=sheet_name, header=1, skiprows=1, nrows=25)\n",
    "    tf=tf.iloc[1:] \n",
    "    tfs=[]\n",
    "    start_col=1 #起始列为1\n",
    "    for i in range(0,31):\n",
    "        end_col = start_col + 5 #结束列为起始列加5\n",
    "        \n",
    "        tf2=tf.iloc[:,start_col:end_col]\n",
    "        start_col=end_col+2 #下一列是前一列间隔2列\n",
    "        \n",
    "        tf2.columns=['分时段类型','现货价格','盈亏','交易电量','电价']\n",
    "        tf2['日期']=i+1\n",
    "        tfs.append(tf2)\n",
    "    tf3=pd.concat(tfs)\n",
    "    tf3['月份']=n_month\n",
    "    \n",
    "    tf3=tf3.reindex(columns=['月份','日期','分时段类型','现货价格','盈亏','交易电量','电价'])\n",
    "    # 在df1中增加两列，分别为'日滚动撮合-交易电量'和'日滚动撮合-电价'\n",
    "    df1['日滚动撮合-交易电量'] = None\n",
    "    df1['日滚动撮合-电价'] = None\n",
    "    # 将tf3的数据按照日期、分时段类型与df1的日期、分时段类型相匹配\n",
    "    df1=df1.merge(tf3[['月份','日期','分时段类型','交易电量','电价']], how='left', left_on=['月份','日期','分时段类型'], right_on=['月份','日期','分时段类型'])\n",
    "    \n",
    "    # 将tf3中的交易电量和电价放到df1中月份为1的'日滚动撮合-交易电量'和'日滚动撮合-电价'数据中，交易电量除以4\n",
    "    \n",
    "    df1['日滚动撮合-交易电量'] = df1['交易电量']/4\n",
    "    df1['日滚动撮合-电价'] = df1['电价']\n",
    "    # 删除不需要的列\n",
    "    df1 = df1.drop(['交易电量', '电价'], axis=1)     \n",
    "    return df1\n",
    "def rqjg(df1,sheet_name,max_day): #将日前价格放到df1中\n",
    "    df=pd.read_excel(file_zcq, sheet_name=sheet_name, usecols='A:AF',nrows=97)\n",
    "    df=df.iloc[:,0:max_day+1]\n",
    "    #将df数据从第2列开始进行逆透视\n",
    "    df=pd.melt(df,id_vars=['时间'],value_vars=df.columns[1:],var_name='日',value_name='日前价格')\n",
    "    #删除时间列为空的行\n",
    "    df=df.dropna(subset=['时间'])\n",
    "    df.rename(columns={'时间':'分钟'},inplace=True)\n",
    "    #添加日期列，数据从日所在字段中的日期数据转换而来\n",
    "    df['日期']=pd.to_datetime(df['日']).dt.day\n",
    "    df1['日前价格']=None\n",
    "    #将df按照日期和时间与df1的日期和时间相匹配,将df中的日前价格放到df1中\n",
    "    df1=df1.merge(df[['日期','分钟','日前价格']],how='left',left_on=['日期','分钟'],right_on=['日期','分钟'])\n",
    "    #删除df1不需要的列\n",
    "    df1=df1.drop(['日前价格_x'],axis=1)\n",
    "    #将tf中日前价格—y列的更名为日前价格\n",
    "    df1.rename(columns={'日前价格_y':'日前价格'},inplace=True)\n",
    "    return df1\n",
    "\n",
    "\n",
    "#请输入月份，放到n_month中\n",
    "n_month=int(input('请输入月份：'))\n",
    "#请输入需调用的中长期持仓文件名\n",
    "file_zcq=input('请输入中长期持仓文件名：')\n",
    "year=2024\n",
    "_,max_day = calendar.monthrange(year, n_month)\n",
    "df_date=zcq(n_month)\n",
    "tf=year_zcq(df_date,'年度多月电量',n_month)\n",
    "df1=ws_zcq(tf,'外送1',n_month,max_day)\n",
    "df1=ws_zcq(df1,'外送2',n_month,max_day)\n",
    "df1=ws_zcq(df1,'外送3',n_month,max_day)\n",
    "df1=ws_zcq(df1,'外送4',n_month,max_day)\n",
    "df1=ws_zcq(df1,'外送5',n_month,max_day)\n",
    "df1=ws_zcq(df1,'外送6',n_month,max_day)\n",
    "df1=ws_zcq(df1,'外送7',n_month,max_day)\n",
    "df1=ws_zcq(df1,'月度国网',n_month,max_day) #月度国网\n",
    "df1=jz_yue(df1,'月、旬集中竞价',n_month,'月度集中竞价') #月度集中竞价\n",
    "df1=jz_sx(df1,'月、旬集中竞价',n_month,'上旬集中竞价') #上旬集中竞价\n",
    "df1=jz_zx(df1,'月、旬集中竞价',n_month,'中旬集中竞价') #中旬集中竞价\n",
    "df1=jz_xx(df1,'月、旬集中竞价',n_month,'下旬集中竞价') #下旬集中竞价\n",
    "df1=jz_yue(df1,'月、旬滚动撮合',n_month,'月度滚动撮合') #月度集中竞价\n",
    "df1=jz_sx(df1,'月、旬滚动撮合',n_month,'上旬滚动撮合') #上旬滚动撮合\n",
    "df1=jz_zx(df1,'月、旬滚动撮合',n_month,'中旬滚动撮合') #中旬滚动撮合\n",
    "df1=jz_xx(df1,'月、旬滚动撮合',n_month,'下旬滚动撮合') #下旬滚动撮合\n",
    "df1=rgd(df1,'日滚动',n_month) #日滚动撮合\n",
    "\n",
    "#df从第6列开始，第6列与第7列相乘，第8列与第9列相乘，一直到最后一列与倒数第二列相乘，然后相加，形成新的一列，列名为“中长期交易收入”\n",
    "df1['中长期交易收入'] = df1.iloc[:, 5::2].mul(df1.iloc[:, 6::2].values).sum(axis=1)\n",
    "#将df中所有包含交易电量的列相加，形成新的一列，列名为“中长期交易电量”\n",
    "df1['中长期交易电量'] = df1.filter(like='交易电量').sum(axis=1)\n",
    "#将中长期交易收入与中长期交易电量相除，形成新的一列，列名为“中长期电价”\n",
    "df1['中长期电价'] = df1['中长期交易收入'] / df1['中长期交易电量']\n",
    "#将df1中增加日前电量\n",
    "df1=rqjg(df1,'日前现货价格（按调控折算后填）',max_day)\n",
    "#计算盈亏情况\n",
    "\n",
    "df1['年度中长期盈亏']=df1['年度中长期1-交易电量']*(df1['年度中长期1-电价']-df1['日前价格']) #年度中长期盈亏\n",
    "df1['度电年度中长期盈亏']=df1['年度中长期盈亏'].div(df1['年度中长期1-交易电量'],fill_value=0) #度电年度盈亏\n",
    "num=7\n",
    "while num>=7:\n",
    "    mz=df1.columns[num]\n",
    "    if mz[:2]=='外送':\n",
    "        #截取mz中的“-”前的字符\n",
    "        mz=mz.split('-')[0]\n",
    "        #df1['京津唐盈亏']=df1['外送京津唐-交易电量']*(df1['外送京津唐-电价']-df1['日前价格'])-df1['外送京津唐-交易电量']*20\n",
    "        df1[f'{mz}盈亏']=df1[f'{mz}-交易电量']*(df1[f'{mz}-电价']-df1['日前价格'])-df1[f'{mz}-交易电量']*20\n",
    "        # #df1['度电京津唐盈亏']=df1['京津唐盈亏'].div(df1['外送京津唐-交易电量'],fill_value=0) #度电京津唐盈亏\n",
    "        # df1[f'度电{mz}盈亏']=df1[f'{mz}盈亏'].div(df1[f'{mz}-交易电量'],fill_value=0)\n",
    "        num=num+2\n",
    "    else:\n",
    "        break\n",
    "df1['月度分时盈亏']=df1['月度集中竞价-交易电量']*(df1['月度集中竞价-电价']-df1['日前价格'])+df1['月度滚动撮合-交易电量']*(df1['月度滚动撮合-电价']-df1['日前价格']) #月度分时交易盈亏\n",
    "#df1['度电月度分时盈亏']=df1['月度分时盈亏'].div((df1['月度集中竞价-交易电量']+df1['月度滚动撮合-交易电量']),fill_value=0) #度电月度分时盈亏\n",
    "df1['上旬分时盈亏']=df1['上旬集中竞价-交易电量']*(df1['上旬滚动撮合-电价'])-df1['日前价格']+df1['上旬滚动撮合-交易电量']*(df1['上旬滚动撮合-电价'])-df1['日前价格'] #上旬分时交易盈亏\n",
    "#df1['度电上旬分时盈亏']=df1['上旬分时盈亏'].div((df1['上旬集中竞价-交易电量']+df1['上旬滚动撮合-交易电量']),fill_value=0) #度电上旬分时盈亏\n",
    "df1['中旬分时盈亏']=df1['中旬集中竞价-交易电量']*(df1['中旬滚动撮合-电价'])-df1['日前价格']+df1['中旬滚动撮合-交易电量']*(df1['中旬滚动撮合-电价'])-df1['日前价格'] #中旬分时交易盈亏\n",
    "#df1['度电中旬分时盈亏']=df1['中旬分时盈亏'].div((df1['中旬集中竞价-交易电量']+df1['中旬滚动撮合-交易电量']),fill_value=0) #度电中旬分时盈亏\n",
    "df1['下旬分时盈亏']=df1['下旬集中竞价-交易电量']*(df1['下旬集中竞价-电价'])-df1['日前价格']+df1['下旬集中竞价-交易电量']*(df1['下旬集中竞价-电价'])-df1['日前价格'] #下旬分时交易盈亏\n",
    "#df1['度电下旬分时盈亏']=df1['下旬分时盈亏'].div((df1['下旬集中竞价-交易电量']+df1['下旬集中竞价-交易电量']),fill_value=0) #度电下旬分时盈亏\n",
    "df1['日滚动盈亏']=df1['日滚动撮合-交易电量']*(df1['日滚动撮合-电价']-df1['日前价格']) #日滚动盈亏\n",
    "#df1['度电日滚动盈亏']=df1['日滚动盈亏'].div(df1['日滚动撮合-交易电量'],fill_value=0) #度电日滚动盈亏\n",
    "df1['中长期盈亏']=df1['中长期交易电量']*(df1['中长期电价']-df1['日前价格']) #中长期盈亏\n",
    "#df1['度电中长期盈亏']=df1['中长期盈亏'].div(df1['中长期交易电量'],fill_value=0) #度电中长期盈亏\n",
    "\n",
    "df1.to_excel(f'盈亏分析/{n_month}月中长期盈亏分析.xlsx', index=False)\n",
    "#"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
